package com.ghs.jms;

import java.net.URI;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

public class ConnUtils {
	
	public static Connection createConnection(String user,String password,URI uri) throws JMSException{
		ConnectionFactory factory = new ActiveMQConnectionFactory(user,password,uri);
		return factory.createConnection();
	}
	
	public static Connection createConnection(URI uri) throws JMSException{
		ConnectionFactory factory = new ActiveMQConnectionFactory(uri);
		return factory.createConnection();
	}
	
	public static Connection createConnection(String brokerUrl) throws JMSException{
		ConnectionFactory factory = new ActiveMQConnectionFactory(brokerUrl);
		return factory.createConnection();
	}
	
	public static void free(Session session,Connection connection){
		if(session!=null){
			try {
				session.close();
			} catch (JMSException e) {
				e.printStackTrace();
			}finally{
				if(connection == null){
					try {
						connection.close();
					} catch (JMSException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
}
